package yiwang.rememberdaka;

import android.os.Environment;

import java.io.File;
import java.io.FileOutputStream;

/**
 * Created by tony on 16/5/9.
 */
public class CheckLog {

    /**
     * 缓存根目录
     */
    public static final File CACHE_ROOT_DIR = new File(android.os.Environment.getExternalStorageDirectory(), "checkinfo");

    /**
     * 保存错误信息到文件中
     *
     * @param checkInfo
     * @return 返回文件名称, 便于将文件传送到服务器 上传服务器待做
     */
    public static boolean saveCheckInfo2File(String checkInfo) {

        StringBuffer sb = new StringBuffer();
        sb.append(checkInfo + "\n");
        try {
            String fileName = "checkinfo" + ".txt";
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                File dir = new File(CACHE_ROOT_DIR, "check");
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                File checkFile = new File(dir, fileName);
                if (!checkFile.exists()) {
                    checkFile.createNewFile();
                }
                FileOutputStream fos = new FileOutputStream(checkFile,true);
                fos.write(sb.toString().getBytes());
                fos.close();
            }
            return true;
        } catch (Exception e) {
        }
        return false;
    }
}
